package cn.dhnep.demo.controller;


import cn.dhnep.demo.pojo.Homepageimg;
import cn.dhnep.demo.pojo.vo.RespBean;
import cn.dhnep.demo.service.SlideshowimgService;
import cn.dhnep.demo.util.Constant;
import cn.dhnep.demo.util.Page;
import cn.dhnep.demo.util.ResultMap;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dhn
 * @since 2021-08-19
 */
@RestController
@RequestMapping("/slideshowimg")
public class SlideshowimgController {

    @Resource
    private SlideshowimgService slideshowimgService;

    private Map<String, Object> result = new HashMap<>();


    @ApiOperation("图片轮播图分页查询")
    @RequestMapping("homepageimglist")
    public ResultMap<List<Homepageimg>> NoticeList(Page page, @RequestParam("limit") Integer limit) throws UnsupportedEncodingException {
        page.setRows(limit);
        List<Homepageimg> orderList = slideshowimgService.selectPageList(page);
        int totals = slideshowimgService.selectPageCount(page);
        page.setTotalRecord(totals);
        return new ResultMap<List<Homepageimg>>("", orderList, 0, totals);
    }


    @ApiOperation("上传产品分类图片")
    @PostMapping("uploadIcon")
    public RespBean uploadIcon(MultipartFile file) {
        if (StringUtils.isEmpty(file)) throw new RuntimeException("图片不能为空！");

        String iconPath = Constant.ICONPATH + "icon/type/";
        System.out.println(iconPath);

        File files = new File(iconPath);
        if (!files.exists() && !files.isDirectory()) {
            System.out.println("目录不存在，创建目录：" + files);
            //如果目录不存在则创建目录
            files.mkdir();
        }

        //String iconPathEnum = "F:/data/"; //本地路径
        //获取原始文件名称（包含格式）
        String originalIconName = file.getOriginalFilename();
        System.out.println("原始图片名称：" + originalIconName);

        //获取文件类型，以最后一个'.'为标识
        String iconType = originalIconName.substring(originalIconName.lastIndexOf(".") + 1);
        System.out.println("图片类型为：" + iconType);

        //设置文件新名称
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        //iconName = newDate + iconName + "." + iconType;
        String iconName = newDate + "." + iconType;
        System.out.println("新文件名称----------->" + iconName);

        //在指定路径下创建文件
        File targetFile = new File(iconPath, iconName);
        String address = "http://39.104.85.153:8081" + iconPath + iconName;

        //将图片保存到指定位置
        try {
            file.transferTo(targetFile);
            System.out.println("上传成功");
            //返回图片的网络路径
            //return RespBean.success(null,address + iconName);
            return RespBean.success(null, address);  //返回本地路径
        } catch (IOException e) {
            return RespBean.error("图片上传失败！");
        }
    }


    @ApiOperation("上传轮播图图片")
    @RequestMapping("addhomepageimg")
    public Map<String, Object> Addhomepageimg(@RequestBody Homepageimg homepageimg, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        int count = slideshowimgService.addimges(homepageimg);
        if (count == 1) {
            result.put("result", "success");
        } else {
            result.put("result", "error");
        }
        return result;
    }


}

